﻿function paging(str) {
    // alert(str.totRecord);
    var tempstr = "";
    if (str.totRecord != "undefined") {
        var ids = "";
        for (var i = 0; i < str.id.length; i++) {
            ids += "'" + str.id[i] + "'";
            if (parseInt(i) != (parseInt(str.id.length) - 1)) {
                ids += ",";
            }
        }
        var fn = "";
        for (var i = 0; i < str.fnName.length; i++) {
            if (str.fnName[i] == "'") {
              fn+= "\\'";
            } else { fn += str.fnName[i]; } 
        }
        var _fstring = "{totRecord:'" + str.totRecord + "',fnName:'" + fn + "',maxShowRecord:'" + str.maxShowRecord + "',noOfLink:'" + str.noOfLink + "',id:[" + ids + "]}"
        if (parseInt(str.totRecord) > 0 && parseInt(str.totRecord) > parseInt(str.maxShowRecord)) {
            var _page = parseInt(str.totRecord) / parseInt(str.maxShowRecord);
            if ((parseInt(str.totRecord) % parseInt(str.maxShowRecord)) > 0) {
                _page = parseInt(_page) + 1;
            }
        }
        if (document.getElementById("_totPages")) {
            document.getElementById("_totPages").value = _page;
        }
        else {
            var _tREle = document.createElement("input");
            _tREle.type = 'hidden';
            _tREle.id = "_totPages";
            _tREle.name = "_totPages";
            _tREle.value = _page;
            document.body.appendChild(_tREle);
        }
        if (document.getElementById("_maxrecord")) {
            document.getElementById("_maxrecord").value = str.maxShowRecord;
        }
        else {
            var _tREle = document.createElement("input");
            _tREle.type = 'hidden';
            _tREle.id = "_maxrecord";
            _tREle.name = "_maxrecord";
            _tREle.value = str.maxShowRecord;
            document.body.appendChild(_tREle);
        }
        if (_page) {
            var _currentPage = 1
            if (document.getElementById('_currentPage')) {
                _currentPage = document.getElementById('_currentPage').value;
            }
            if (_currentPage > 1) {
                tempstr += '<a href="javascript:void(0)" onclick="change_page(this,' + _fstring + ');' + str.fnName + '" >First</a>';
                tempstr += '<a href="javascript:void(0)" onclick="change_page(this,' + _fstring + ');' + str.fnName + '" >Pre</a>';
            }
            var _new_start = 0;
            if (parseInt(_page) != parseInt(_currentPage)) {
                if (parseInt(str.noOfLink) > parseInt(_page)) {
                    str.noOfLink = _page;
                } else {
                    if (parseInt(_currentPage) != 1) {
                        str.noOfLink = parseInt(str.noOfLink) + (parseInt(_currentPage)-1)
                        if (parseInt(str.noOfLink) > parseInt(_page)) {
                            str.noOfLink = _page;
                        }
                    }
                }
            }
            else {
                str.noOfLink = _page;
            }
            if (parseInt(str.noOfLink) > 5) {
                _new_start = (parseInt(str.noOfLink) - 5);
            }
            for (var i = parseInt(_new_start); i < str.noOfLink; i++) {
                if (i == (parseInt(_currentPage) - 1)) {
                    tempstr += '<b>' + (i + 1) + '</b>';
                }
                else {
                    tempstr += '<a href="javascript:void(0)" onclick="change_page(this,' + _fstring + ');' + str.fnName + '" >' + (i + 1) + '</a>';
                }
            }
            if (parseInt(_currentPage) != parseInt(_page)) {
                tempstr += '<a href="javascript:void(0)" onclick="change_page(this,' + _fstring + ');' + str.fnName + '" >Next</a>';
                tempstr += '<a href="javascript:void(0)" onclick="change_page(this,' + _fstring + ');' + str.fnName + '" >Last</a>';
            }
        }
    }
    for (var i = 0; i < str.id.length; i++) {
        if (document.getElementById(str.id[i])) {
            document.getElementById(str.id[i]).innerHTML = tempstr;
        }
    }
}
function change_page(_cntrl, str) {
    var inp = "";
    if (document.getElementById('_currentPage')) {
        inp = document.getElementById('_currentPage');
    }
    else {
        inp = document.createElement('input');
        inp.type = 'hidden';
        inp.id = '_currentPage';
        inp.name = '_currentPage';
        inp.value = "1";
        document.body.appendChild(inp);
    }
    var _totPages;
    if (document.getElementById('_totPages')) {
        _totPages = document.getElementById('_totPages').value;
    }
    else {
        _totPages = 1;
    }

    // var _val = document.getElementById(_cntrl.id);
    if (_cntrl.innerHTML == "Next") {
        inp.value = parseInt(inp.value) + 1;
    }
    else if (_cntrl.innerHTML == "Pre") {
        inp.value = parseInt(inp.value) - 1;
    }
    else if (_cntrl.innerHTML == "First") {
        inp.value = 1;
    }
    else if (_cntrl.innerHTML == "Last") {
        inp.value = _totPages;
    }
    else {
        inp.value = _cntrl.innerHTML;
    }
    paging(str);
}